// Copyright © 2017 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
	"license/pkg/client"
	"license/pkg/notice"
	"license/pkg/server"
	"log"

	"github.com/spf13/cobra"
)

var (
	LicenseId int
	Alllist,JsonFile,Url,Authorization string
)

//删除license id
var DeleteCmd = &cobra.Command{
	Use:   "delete",
	Short: "delete license",
	Long:  `delete license`,
	Run: func(cmd *cobra.Command, args []string) {
		client.LmDelete(LicenseId)
	},
}
//查询license
var GetCmd = &cobra.Command{
	Use:   "get",
	Short: "get license",
	Long:  `get license`,
	Run: func(cmd *cobra.Command, args []string) {
		client.LmGet(LicenseId)
	},
}
//添加更新license
var ApplyCmd = &cobra.Command{
	Use:   "apply",
	Short: "add license/update license",
	Long:  `"add license/update license"`,
	Run: func(cmd *cobra.Command, args []string) {
		client.LmApply(JsonFile)
	},
}

//api方式登录并生成配置文件
var LoginCmd = &cobra.Command{
	Use:   "login",
	Short: "login license server",
	Long:  `"login license server"`,
	Run: func(cmd *cobra.Command, args []string) {
		client.ApiLogin(Url,Authorization)
	},
}


var BuildCmd = &cobra.Command{
	Use:   "build",
	Short: "build license tools",
	Long:  `"build license tools"`,
	Run: func(cmd *cobra.Command, args []string) {
		log.Println("build not supprt ")
	},
}


var DownLoadCmd = &cobra.Command{
	Use:   "download",
	Short: "download private ciphertext",
	Long:  `"download private ciphertext"`,
	Run: func(cmd *cobra.Command, args []string) {
		   client.DownloadRsa(LicenseId)
	},
}

var ServerCmd = &cobra.Command{
	Use:   "server",
	Short: "start license server",
	Long:  `"start license server"`,
	Run: func(cmd *cobra.Command, args []string) {
		   go notice.NoticeMail(JsonFile)
           server.HTTPMain(JsonFile)
	},
}


func init() {
	DeleteCmd.Flags().IntVarP(&LicenseId,"id","",0,"license id")
	DeleteCmd.MarkFlagRequired("LicenseId")
	RootCmd.AddCommand(DeleteCmd)
	GetCmd.Flags().IntVarP(&LicenseId,"id","",0,"license id") //查询单个
	RootCmd.AddCommand(GetCmd)
	ApplyCmd.Flags().StringVarP(&JsonFile,"file","f","","") //指定json 格式文件
	RootCmd.AddCommand(ApplyCmd)
	LoginCmd.Flags().StringVarP(&Url, "Url", "", "","http://xxxx.xxx")
	LoginCmd.Flags().StringVarP(&Authorization, "Authorization", "","", "user token")
	LoginCmd.MarkFlagRequired("Url")
	LoginCmd.MarkFlagRequired("Authorization")
	RootCmd.AddCommand(LoginCmd)
	RootCmd.AddCommand(BuildCmd) //编译工具镜像 内部包含私钥 以及加密字符串
	DownLoadCmd.Flags().IntVarP(&LicenseId,"id","",0,"license id")
	DownLoadCmd.MarkFlagRequired("LicenseId")
	RootCmd.AddCommand(DownLoadCmd) //下载证书以及加密字符串
	ServerCmd.Flags().StringVarP(&JsonFile,"file","f","/app/config/config.json","config.json default /app/config/config.json") //指定json 格式文件
	LoginCmd.MarkFlagRequired("JsonFile")
	RootCmd.AddCommand(ServerCmd)
}